package com.base.cn.platform.os.controller.web.order.uc;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.order.OrderStatus;
import com.base.cn.platform.os.common.utils.order.PayType;
import com.base.cn.platform.os.entity.order.order.OrderCondition;
import com.base.cn.platform.os.service.manage.order.goods.OrderBookService;
import com.base.cn.platform.os.service.manage.order.goods.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 前台用户订单Controller
 *
 * @author s.li
 * @create 2018-05-29-9:57
 */
@Controller
@RequestMapping("/web/uc/order")
public class WebUserOrderController extends WebBaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderBookService orderBookService;

    /**
     * 初始化订单列表
     * @return
     */
    @RequestMapping("/list")
    public String orderList(){
        return resultPage("/order/uc/user-order-list");
    }

    /**
     * 加载订单数据
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxOrderList")
    public String ajaxOrderList(Model model,
                                HttpServletRequest request,
                                @RequestBody Map<String, Object> params,
                                @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Object userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId",userId);
        params.put("status",1);
        params.put("notPayType","'"+ PayType.PAY_TYPE_FREE +"','"+PayType.PAY_TYPE_COURSE_CART+"','"+PayType.PAY_TYPE_MEMBER+"'");
        Map<String,Object> dataMap = orderService.findWebOrderPage(params,currentPage,5,false,true,true,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/order/uc/ajax-user-order-list");
    }

    /**
     * 查看订单详情
     * @param model
     * @param orderId 订单ID
     * @return 订单详情页面
     */
    @RequestMapping("/orderInfo")
    public String orderInfo(Model model,
                            @RequestParam("orderId") BigDecimal orderId){
        OrderCondition condition = new OrderCondition();
        condition.setId(orderId);
        condition.setSetUserData(true);
        condition.setSetDetailsData(true);
        condition.setSetShopData(true);
        condition.setSetOrderBook(true);
        Map<String,Object> order = orderService.findOrderInfo(condition);
        model.addAttribute("order",order);
        return resultPage("/order/uc/user-order-info");
    }

    /**
     * 删除订单
     * @param request
     * @param orderId
     * @return
     */
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public Map<String,Object> deleteOrder(HttpServletRequest request,
                                          @RequestParam("orderId") BigDecimal orderId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return orderService.deleteOrder(orderId.toString(),userId);
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @RequestMapping("/cancelOrder")
    @ResponseBody
    public Map<String,Object> cancelOrder(@RequestParam("orderId") BigDecimal orderId){
        return orderService.updateOrderStatus(new BigDecimal(0),orderId,"CANCEL",null,null,null,null,null,null);
    }

    /**
     * 签收订单
     * @param orderId 订单号
     * @return Map<String,Object>
     */
    @RequestMapping("/signOrder")
    @ResponseBody
    public Map<String,Object> signOrder(HttpServletRequest request,
                                        @RequestParam("orderId") BigDecimal orderId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return orderBookService.updateSignStatus(userId,orderId,2);
    }

    /**
     * 验证订单状态
     *
     * @param orderNo 订单号
     * @return
     */
    @RequestMapping("/ajax/verificationOrderStatus")
    @ResponseBody
    public Map<String, Object> verificationOrderStatus(@RequestParam("orderNo") String orderNo) {

        Map<String, Object> order = orderService.findOrderBuyNo(orderNo);
        String orderStatus = OrderStatus.INIT;
        if (ObjectUtils.isNotEmpty(order)) {
            orderStatus = (String) order.get("orderStatus");
        }
        order.put("orderStatus",orderStatus);
        return order;
    }

}
